<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="tag" tagdir="/WEB-INF/tags" %>
<tag:header/>
<div id="pageGridSelect"></div>
<script type="text/template" id="pageGridTmplSelect">
    <form>
        <div class="col">
            <input id="checkCityId" data-qfield="checkCityId" class="easyui-combotree query" data-qoperator="IN"
                   data-qtype="STRING"
                   data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/api/ebp/sysstructure/getusertree',required:false,multiple:true">
        </div>

        <div class="col">
            <input id="userType" data-qfield="userType" data-qoperator="EQUAL" data-qtype="STRING" class="easyui-combobox query"
                   data-options="editable:false,valueField:'value',
								  textField:'text',
								  url:'${pageContext.request.contextPath}/api/ebms/codeMaster/getchildlistcombobox?codeGroupId=UserType'">
        </div>

        <div class="col">
            <input id="realName" data-qfield="realName" class="query" data-qoperator="LIKE" data-qtype="STRING"/>
        </div>

        <div class="col">
            <input id="cellphone" data-qfield="cellphone" class="query" data-qoperator="EQUAL" data-qtype="STRING"/>
        </div>

        <div class="col">
            <select id="userStatus" data-qfield="userStatus" class="easyui-combobox query"
                    data-options="editable:false"
                    data-qoperator="EQUAL" data-qtype="INT">
                <option value="">全部</option>
                <option value="0">启用</option>
                <option value="1">停用</option>
            </select>
        </div>
    </form>
</script>
<script type="text/javascript">
    var pagegrid;
    var items = new Array();
    var i = 1;
    (function () {
        //参数配置
        var options = {
            containerId: 'pageGridSelect',
            pageGridTmplId: 'pageGridTmplSelect',
            moduleName: '推荐人多选放大镜',
            useCommonApi: false,
            contextUrl: '${pageContext.request.contextPath}',
            module: 'ebms',
            submodule: 'sellerInfo',
            instantSearch: true,
            noPermissionBtn: true,
            hideCheckBox: false,
            viewName:'selectRecommenders'
        };

        options.datagridOptions = {
            onCheck: function (index, row) {
                addItem(row, items);
            },
            onUncheck: function (index, row) {
                deleteItem(row, items);
            },
            onCheckAll: function (rows) {
                for (var index in rows) {
                    addItem(rows[index], items);
                }
            },
            onUnCheckAll: function (rows) {
                for (var index in rows) {
                    deleteItem(rows[index], items);
                }
            }
        };

        options.afterParse = function () {
            $('#beginDate').datebox('setValue', moment(new Date()).subtract(3, 'months').format('YYYY-MM-DD'));
            $('#endDate').datebox('setValue', moment(new Date()).format('YYYY-MM-DD'));
            pagegrid.listen('onLoadSuccess', function (data) {
                var oldLoadSuccess = pagegrid.$datagrid.datagrid('options')['onLoadSuccess'];
                if (oldLoadSuccess != 'function') {
                    checkList(data.rows, pagegrid.$datagrid);
                } else {
                    oldLoadSuccess();
                    checkList(data.rows, pagegrid.$datagrid);
                }

                $('div.toolbar-btnBar').last().html('<span id="checkedValue"></span><a href="javascript:void(0)" onclick="ok()" class="easyui-linkbutton l-btn l-btn-small l-btn-plain" iconcls="icon-add" plain="true"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">确定</span><span class="l-btn-icon icon-ok">&nbsp;</span></span></a>');

                $('#checkedValue').html("已选中：" + items.length + "条记录  ");

                pagegrid.$datagrid.datagrid('resize');
            });
        };

        //=================grid加载成功后方法=================START
        options.onPageLoadSuccess = function (res) {
            // var _rows = res.rows;
            // 取消勾选当前页中的所有行
            pagegrid.$datagrid.datagrid('uncheckAll');
            items = new Array();
            $('#checkedValue').html("已选中：" + 0 + "条记录  ");
        }
        //=================grid加载成功后方法=================END

        //初始化
        pagegrid = new ehsure.PageGrid(options);
        pagegrid.dataApi = pagegrid.options.contextUrl + "/api/ebms/sellerInfo/list";
        pagegrid.build();

    })();

    function inArray(obj, array) {
        for (var index in items) {
            if (items[index].ID == obj.ID) {
                return index;
            }
        }
        return -1;
    }

    function addItem(item, items) {
        if (inArray(item, items) == -1) {
            items.push(item);
            $('#checkedValue').html("已选中：" + items.length + "条记录  ");
        }
    }

    function deleteItem(item, items) {
        if (inArray(item, items) > -1) {
            items.splice(inArray(item, items), 1);
            $('#checkedValue').html("已选中：" + items.length + "条记录  ");
        }
    }

    function checkList(rows, dg) {
        for (var i = 0; i < rows.length; i++) {
            var obj = rows[i];
            if (inArray(obj, items) > -1) {
                obj.checked = true;
            }
        }
        $.each(rows, function (index, item) {
            if (item.checked) {
                dg.datagrid('checkRow', index);
            }
        });
    }

    function ok() {
        if (items.length == 0) {
            $.messager.alert('警告', "请选择数据！", "warning");
            return;
        }

        // var row = items[0];
        // var ids = "";
        // var phones = "";
        // for (var item in items) {
        //     ids += items[item].ID + ","
        //     phones += items[item].CELLPHONE + ","
        // }
        // row.ID = ids.substring(0, ids.length - 1);
        // row.PHONE = phones.substring(0, ids.length - 1);
        // parent.$("#recommenderIds").val(row.ID);
        // parent.$("#recommenderPhones").val(row.PHONE);

        appendRow();

        parent.$('#msgwindow').window('close');
    }


    // 父页面列表追加方法 add by ken 2018-3-8
    function appendRow(){

        var rows = pagegrid.getMultiSelected();
        //  获取此页面选中
        var _memberIds = new Array();
        var _memberPhones = new Array();
        $.each(rows,function(_index,_object){
            _memberIds.push(_object.ID);
            _memberPhones.push(_object.CELLPHONE);
        });
        // 获取父页面 已有数据
        var t1 = parent.$("#recommenderIds").val();
        var t2 = parent.$("#recommenderPhones").val();
        var _parent_memberIds = new Array();
        var _parent_memberPhones = new Array();
        if("" != t1){
            _parent_memberIds = t1.split(',');

        }
        if("" != t2){
            _parent_memberPhones = t2.split(',');
        }
        // concat 合并 当前页面数据 和 父页面 数组

        var _cArr1 = _memberIds.concat(_parent_memberIds);
        var _cArr2 = _memberPhones.concat(_parent_memberPhones);

        // 去重 2个页面数据
        var set1 = new Set(_cArr1);
        var set2 = new Set(_cArr2);

        // 回写到 父页面
        var _dest_memberIds = Array.from(set1);
        var _dest_memberPhones = Array.from(set2);
        var _dest_memberIds_str = _dest_memberIds.join(',');
        var _dest_memberPhones_str = _dest_memberPhones.join(',');
        parent.$("#recommenderIds").val(_dest_memberIds_str);
        parent.$("#recommenderPhones").val(_dest_memberPhones_str);

    }

</script>
